package com.demo.proxy.demo;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JdkProxy implements InvocationHandler {

    private Class<?> target;

    public JdkProxy(Class<?> target)
    {
        this.target = target;
    }

    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //System.out.println("方法名:" + proxy);
        System.out.println("方法名:" + method.getName());
        return method.invoke(new StudentImpl(), args);
    }

    /*    public static Object  test(Class<?> target) {
            JdkProxy proxy = new JdkProxy(target);
            return Proxy.newProxyInstance(JdkProxy.class.getClassLoader(),
                    new Class<?>[]{target}, proxy);
        }*/

    public static void main(String[] args){
        JdkProxy proxy = new JdkProxy(StudentInterface.class);
        StudentInterface studentInterface = (StudentInterface) Proxy.newProxyInstance(JdkProxy.class.getClassLoader(),
                new Class<?>[]{StudentInterface.class}, proxy);
        studentInterface.say("123");
    }

}
